#include "error.h"
#include <winhttp.h>

#include "debug.h"
#include "string.h"
#include "crash_if_specific_error.h"

int g_error_extra_code1 = 0;

int error_extra_code1()
{
	return g_error_extra_code1;
}

void set_error_extra_code1(int extra_code)
{
	g_error_extra_code1 = extra_code;
}

HRESULT HRESULTFromLastError()
{
	DWORD error_code = ::GetLastError();
	ASSERT1(error_code != NO_ERROR);
	HRESULT hr = error_code != NO_ERROR ? HRESULT_FROM_WIN32(error_code) : E_FAIL;
	CRASH_IF_SPECIFIC_ERROR(hr);
	return hr;
}

HRESULT HRESULTFromHttpStatusCode(int status_code)
{
	COMPILE_ASSERT(AGENT_E_NETWORK_FIRST + HTTP_STATUS_LAST <
		AGENT_E_NETWORK_LAST,
		GOOPDATE_E_NETWORK_LAST_too_small);
	HRESULT hr = S_OK;
	if (HTTP_STATUS_FIRST <= status_code && status_code <= HTTP_STATUS_LAST) {
		hr = AGENT_E_NETWORK_FIRST + status_code;
	} else {
		hr = AGENT_E_NETWORK_LAST;
	}
	ASSERT1(HRESULT_FACILITY(hr) == FACILITY_ITF);
	return hr;
}